package com.gxa.jjty.cjr.service.impl;

import com.gxa.jjty.cjr.mapper.AppServeMapper;
import com.gxa.jjty.cjr.service.ApplicationServeService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.AppServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/12/02 12:27
 * Description:
 * Version: V1.0
 */
@Service
public class ApplicationServeServiceImpl implements ApplicationServeService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired(required = false)
    private AppServeMapper applicationServeMapper;

    private static final String SERVE_LIST="serveList:";

    @Override
    public Map<String, Object> findAllServe(Long id) {
//        ListOperations<String, Object> listOps = redisTemplate.opsForList();
//        List<Object> serveList = listOps.range(SERVE_LIST+id, 0, -1);
        Object serveList = redisTemplate.opsForValue().get(SERVE_LIST + id);
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(serveList);
        if (serveList==null){
            Map<String,List<AppServe>> appServes= new HashMap<>(16);
            List<AppServe> concerned = applicationServeMapper.getConcerned(id);
            List<AppServe> notConcerned = applicationServeMapper.getNotConcerned(id);
            appServes.put("concerned",concerned);
            appServes.put("notConcerned",notConcerned);
            jsonResult.setData(appServes);
            redisTemplate.opsForValue().set(SERVE_LIST + id,appServes);
        }
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String, Object> concerned(Long handicappedId, Long appServeId) throws BusinessException {
        int concerned = applicationServeMapper.concerned(handicappedId, appServeId);
        if (concerned!=1){
            throw new BusinessException(ExceptionCode.CONCERNED_FAILED);
        }
        redisTemplate.delete(SERVE_LIST+handicappedId);
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("关注成功!");
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String, Object> unconcerned(Long handicappedId, Long appServeId) throws BusinessException {
        int unconcerned = applicationServeMapper.unconcerned(handicappedId, appServeId);
        if (unconcerned!=1){
            throw new BusinessException(ExceptionCode.LOCK_FAILED);
        }
        redisTemplate.delete(SERVE_LIST+handicappedId);
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("取关成功!");
        return jsonResult.getDataMap();
    }
}
